package jhta;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.SwingConstants;

import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.Icon;
import javax.swing.JLayeredPane;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.UIManager;
import javax.swing.JFormattedTextField;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.Format;
import java.util.Vector;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextArea;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.SystemColor;

public class CodeInput extends JInternalFrame {
	private int index;   //순번
	private String major; //대분류
	private String minor; //소분류


	JavaDBConnection jdb= new JavaDBConnection();

	private JPanel panel;
	private JLabel tit;
	private JPanel panel_1;
	private JLayeredPane layeredPane;
	private JComboBox comMajor;
	private JLabel label_1;
	private JFormattedTextField addMinor;
	private JFormattedTextField addMajor;
	private JButton MajorSave;
	private JLabel label;
	private JComboBox comMinor;
	private JLabel label_2;
	private JLabel label_3;
	private JButton MinorSave;
	private JScrollPane scrollPane;
	private JTextArea txtrA;



	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public String getMinor() {
		return minor;
	}
	public void setMinor(String minor) {
		this.minor = minor;
	}
	public CodeInput(){
		this(1);
		setFont(new Font("맑은 고딕", Font.BOLD, 12));

	}
	public CodeInput(int g) {
		super("도서분류 코드입력", true, true, false, true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		setBounds(100,100,601,460);
		this.index = g;
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		setResizable(false);
		bigCom();
		smallCom();

	}
	public void bigCom(){
		//대분류 콤보박스 넣기

		String sql="select code_name from code where code_big like 'A%'and code_small is null order by code_no ";//대분류에 있는 글자만 뽑는다 

		try{
			PreparedStatement pstmt = jdb.getConn().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();//담아올래

			while(rs.next()){

				comMajor.addItem(rs.getString(1)); //대분류 콤보박스에 전체가 들어갑니다.

			}
			rs.close();
			pstmt.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void smallCom(){

		//소분류 콤보박스 

		try{
			String sql2="select code_name from code where code_small like 'B%'  ";//소분류에 있는 글자만 뽑	

			PreparedStatement pstmt = jdb.getConn().prepareStatement(sql2);
			ResultSet rs = pstmt.executeQuery();//담아올래

			while(rs.next()){

				comMinor.addItem(rs.getString(1)); //소분류 콤보박스에 전체가 들어갑니다.

			}
			rs.close();
			pstmt.close();

		}catch(Exception ex){
			ex.printStackTrace();
		}
	}



	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLabel_4(), BorderLayout.NORTH);
			panel.add(getPanel_1(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JLabel getLabel_4() {
		if (tit == null) {
			Image img1 = Toolkit.getDefaultToolkit().getImage("title.jpg");
			img1 = img1.getScaledInstance(580, 62, 0);
			ImageIcon icon1 = new ImageIcon(img1);
			tit = new JLabel(icon1);
			tit.setBounds(0, 0, 580, 62);
		}
		return tit;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.add(getLayeredPane_1());
		}
		return panel_1;
	}
	private JLayeredPane getLayeredPane_1() {
		if (layeredPane == null) {
			layeredPane = new JLayeredPane();
			layeredPane.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			layeredPane.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\uCF54\uB4DC\uC785\uB825", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
			layeredPane.setBounds(12, 10, 561, 350);
			layeredPane.add(getComMajor());
			layeredPane.add(getLabel_1());
			layeredPane.add(getAddMinor());
			layeredPane.add(getAddMajor());
			layeredPane.add(getMajorSave());
			layeredPane.add(getLabel_3());
			layeredPane.add(getComMinor());
			layeredPane.add(getLabel_2_1());
			layeredPane.add(getLabel_3_1());
			layeredPane.add(getMinorSave());
			layeredPane.add(getScrollPane_1());
		}
		return layeredPane;
	}

	private JComboBox getComMajor() {
		if (comMajor == null) {
			comMajor = new JComboBox();
			comMajor.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			comMajor.addItem("전체");
			comMajor.setBounds(114, 21, 116, 26);
			comMajor.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent arg0) {

					if(comMinor!=null){
						comMinor.removeAllItems(); //콤보박스 다 지운다.

					}
					String comb = (String)comMajor.getSelectedItem();

					if(!comb.equals("전체")){

						try{
							String sql2="select code_name from code where code_big=(select code_big from code where code_name='"+comb+"' "
									+ "and code_small is null) and code_small is not null  ";//소분류에 있는 글자만 뽑	

							PreparedStatement pstmt = jdb.getConn().prepareStatement(sql2);
							ResultSet rs = pstmt.executeQuery();//담아올래

							while(rs.next()){

								comMinor.addItem(rs.getString(1)); //대분류 콤보박스에 전체가 들어갑니다.

							}
							rs.close();
							pstmt.close();

						}catch(Exception ex){
							ex.printStackTrace();
						}

					}if (comb.equals("전체")){//전체라면
						smallCom();
					}

				}
			});


		}
		return comMajor;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("대분류 추가");
			label_1.setForeground(Color.BLACK);
			label_1.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			label_1.setHorizontalAlignment(SwingConstants.RIGHT);
			label_1.setBounds(24, 63, 78, 15);
		}
		return label_1;
	}
	private JFormattedTextField getAddMinor() {
		if (addMinor == null) {
			addMinor = 
					new JFormattedTextField((Format) null);
			addMinor.setBounds(114, 150, 345, 26);
		}
		return addMinor;
	}
	private JFormattedTextField getAddMajor() {
		if (addMajor == null) {
			addMajor = new JFormattedTextField((Format) null);
			addMajor.setBounds(114, 57, 116, 28);
		}
		return addMajor;
	}
	private JButton getMajorSave() {
		if (MajorSave == null) {
			MajorSave = new JButton("저장");
			MajorSave.setOpaque(false);
			MajorSave.setBackground(new Color(255, 250, 205));
			MajorSave.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			MajorSave.setForeground(Color.BLACK);
			MajorSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {



					if(!addMajor.getText().equals("")){

						major = addMajor.getText().trim(); //추가할 대분류

						boolean bigFlag = false; 
						for(int i=0;i<=comMajor.getItemCount();i++){



							if(major.equals(comMajor.getItemAt(i))){

								bigFlag = true;
								JOptionPane.showMessageDialog(null, major+"라는 이미 동일한 값이 있습니다.");
								addMajor.setText("");
								break;

							}else{

							}
						}


						if(!bigFlag){//중복이 아닌 값

							String sql = "insert into code(code_no, code_big , code_name) values(code_sequence.nextval, 'A'||big_sequence.nextval ,'"+major+"')";
							//순번 , 대분류 , 대분류명   

							try{
								PreparedStatement pstmt = jdb.getConn().prepareStatement(sql);
								pstmt.execute();
								pstmt.close();
							}catch(Exception ex){
								ex.printStackTrace();
							}
							comMajor.addItem(major);//콤보박스 추가
							JOptionPane.showMessageDialog(null, major+"가 대분류에 입력되었습니다");
							addMajor.setText("");
						}


					}else{
						JOptionPane.showMessageDialog(null, " 대분류 명을 입력주세요 ");

					}




				}
			});
			MajorSave.setBounds(242, 57, 78, 26);
		}
		return MajorSave;
	}
	private JLabel getLabel_3() {
		if (label == null) {
			label = new JLabel("대분류    ");
			label.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			label.setHorizontalAlignment(SwingConstants.RIGHT);
			label.setBounds(39, 27, 63, 15);
		}
		return label;
	}
	private JComboBox getComMinor() {
		if (comMinor == null) {
			comMinor = new JComboBox();
			comMinor.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			comMinor.addItem("전체");
			comMinor.setBounds(114, 114, 345, 26);
		}
		return comMinor;
	}
	private JLabel getLabel_2_1() {
		if (label_2 == null) {
			label_2 = new JLabel("소분류    ");
			label_2.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			label_2.setHorizontalAlignment(SwingConstants.RIGHT);
			label_2.setBounds(39, 120, 63, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3_1() {
		if (label_3 == null) {
			label_3 = new JLabel("소분류 추가");
			label_3.setForeground(Color.BLACK);
			label_3.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			label_3.setHorizontalAlignment(SwingConstants.RIGHT);
			label_3.setBounds(24, 155, 78, 15);
		}
		return label_3;
	}
	private JButton getMinorSave() {
		if (MinorSave == null) {
			MinorSave = new JButton("저장");
			MinorSave.setOpaque(false);
			MinorSave.setBackground(new Color(255, 250, 205));
			MinorSave.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			MinorSave.setForeground(Color.BLACK);
			MinorSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					if(!addMinor.getText().equals("")){



						if(comMajor.getSelectedIndex() >=1){

							boolean smallFlag = false;
							minor = addMinor.getText().trim(); //추가할 소분류명
							for(int i=0;i<=comMinor.getItemCount();i++){


								if(minor.equals(comMinor.getItemAt(i))){								
									smallFlag = true;
									JOptionPane.showMessageDialog(null, minor+"라는 이미 동일한 값이 있습니다.");
									addMinor.setText("");
									break;
								}
							}
							if(!smallFlag){ //동일한게 없으면 

								major = (String)comMajor.getSelectedItem();//콤보를 찍은것의 대분류 값

								String sql = "insert into code(code_no, code_big ,code_small, code_name) values(code_sequence.nextval,"
										+ "(select code_big from code where code_name = '"+major+"')  ,'B'||small_sequence.nextval, '"+minor+"')";
								//순번 , 대분류 , 대분류명   

								try{
									PreparedStatement pstmt = jdb.getConn().prepareStatement(sql);
									pstmt.execute();
									pstmt.close();
								}catch(Exception ex){
									ex.printStackTrace();
								}
								comMinor.addItem(minor);//소분류 콤보박스 추가
								JOptionPane.showMessageDialog(null, minor+"가 소분류에 입력되었습니다");
								addMinor.setText("");
							}


						}else{
							JOptionPane.showMessageDialog(null, "대분류 항목을 선택해주세요.");
						}
					}else{
						JOptionPane.showMessageDialog(null, " 소분류 명을 입력주세요 ");

					}



				}
			});
			MinorSave.setBounds(471, 149, 78, 26);
		}
		return MinorSave;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 186, 537, 154);
			scrollPane.setViewportView(getTextArea_1());
		}
		return scrollPane;
	}
	private JTextArea getTextArea_1() {
		if (txtrA == null) {
			txtrA = new JTextArea();
			txtrA.setBackground(new Color(255, 255, 224));
			txtrA.setEditable(false);
			txtrA.setForeground(Color.BLUE);
			txtrA.setFont(new Font("맑은 고딕", Font.PLAIN, 14));
			txtrA.setText(" * 대분류 저장시 A1부터 코드로 저장됩니다.\r\n - 대분류 추가 입력상자에 값을 입력후 저장을 클릭시 추가 됩니다.\r\n - (기본 저장된 코드) A1 : 도서  A2 : 관리자  A3 : 기수\r\n\r\n * 소분류 저장시 B1부터 시작하며 대분류를 선택 후 저장이 가능 합니다.\r\n - 대분류 선택 후 추가 할 소분류 명을 입력후 저장을 클릭시 추가 됩니다. ");
		}
		return txtrA;
	}
}
